<?php
namespace Client\Table;

use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;

use Zend\Paginator\Paginator;
use Zend\Db\Sql\Where;
use Zend\Db\ResultSet\ResultSet;

class OrganizationTable extends CommonTable
{
    protected $table = 'hy_organization';
    protected $id = 'org_id';
    
    public function fetchAll($cate_id, $limit, $paginated = false) {
    	
    }
    

    public function orgSearch($query){
    	
    	$select = new Select(array('o' => 'hy_organization'));
    	$select->columns(array('org_id', 'org_name', 'org_thumb_image'));
		if(!empty($query['keyword'])) {
			$spec = function (Where $where) use ($query) {
				$where->like('o.org_name', "%".$query['keyword']."%");
			};
        
			$select->where($spec);
		}
    	$select->order('o.org_level ASC');
    	$rowset = $this->selectWith($select);
    	$list = $rowset->toArray();
    	return $list;
    }
    
    public function getItems($where,$limit){
    	$select = new Select(array('o' => 'hy_organization'));
    	$select->columns(array('org_id','org_type','org_name', 'org_thumb_image'));
    	if($where['catalog_id']){
    		$select->where(array('catalog_id'=>$where['catalog_id']));
    	}
    	$select->order('o.org_level ASC');
    	$query=$select->getSqlString($this->adapter->getplatform());
    	return $this->fetchPaginate($select, $query);
    }
    
    

    /**
     * 根据ID取单位信息
     * @param unknown $id
     */
    public function getInfoById($id)
    {
        $select = new Select(array('o' => $this->getTable()));
        $select->where(array('o.org_id' => $id));
        $rowset = $this->selectWith($select);
        $row = $rowset->current();
        return $row;
    }

    public function findByCateId($query = array()) {
        
        $select = new Select(array('o' => 'hy_organization'));
        $select->columns(array('org_id', 'org_name', 'org_thumb_image'));

        $select->join(
            array('p' => 'hy_product'),
            'p.org_id=o.org_id',
            array('product_id', 'product_name'),
            $select::JOIN_LEFT
            );

            if(!empty($query['keyword'])) {
                $spec = function (Where $where) use ($query) {
                    $where->like('o.org_name', "%".$query['keyword']."%");
                };
        
                $select->where($spec);
            }
        $select->where(array('p.recommend' => 1));
        $select->order('o.org_level ASC');
        return $this->fetchPaginate($select, $query);
    }
}